package tpl

import (
	"fmt"
	"gitee.com/alex_li/amazon-rpt/internal/rpt"
	"gitee.com/alex_li/amazon-rpt/internal/sdk/advertising"
	"gitee.com/alex_li/amazon-rpt/internal/sdk/dict"
	"gitee.com/alex_li/amazon-rpt/internal/tool"
)

func New(adProduct dict.AdProductType, rptType dict.ReportType,
	unit dict.RptTimeUnit, filter ...advertising.RptFilter) (t *rpt.Template) {
	t = &rpt.Template{
		Product:  adProduct,
		Group:    dict.GetGroupBy(rptType, adProduct),
		Columns:  dict.GetRptColumns(rptType, adProduct, unit),
		RptType:  dict.GetReportTypeId(rptType, adProduct),
		TimeUnit: unit,
		Filters:  filter,
	}

	t.Name = fmt.Sprintf("%s%s%s", adProduct.Name, unit.Name, rptType)
	t.Name = "rpt_" + tool.CamelToSnake(t.Name)
	return
}

var templateMap = make(map[string]rpt.Template)

func registerTpl(template *rpt.Template) {
	templateMap[template.GetTplName()] = *template
}

func GetTpl(tplName string) (temp rpt.Template, err error) {
	temp, ok := templateMap[tplName]
	if !ok {
		err = fmt.Errorf("tpl %s not found", tplName)
		return
	}
	return
}

func GetTplList() (list []string) {
	for name := range templateMap {
		list = append(list, name)
	}
	return
}
